package com.zzccyy.jpdemo.page

import android.content.Intent
import android.view.View
import android.widget.Button
import com.sztle.admodule.IEnterHomeCallback
import com.sztle.admodule.apiprocessor.OutIntAdApi
import com.sztle.admodule.outstatus.AppStatusProcessor
import com.zzccyy.jpdemo.NoScrollViewPager
import com.zzccyy.jpdemo.R
import com.zzccyy.jpdemo.ViewPagerAdapter
import com.zzccyy.jpdemo.base.BasicBaseActivity

class HomeActivity : BasicBaseActivity(), View.OnClickListener {
    private var viewPager: NoScrollViewPager? = null
    private var btnPage1: Button? = null
    private var btnPage2: Button? = null
    private var btnPage3: Button? = null
    private var btnPage4: Button? = null

    override fun isNeedAd(): Boolean {
        return false
    }

    override val layoutId: Int
        get() = R.layout.activity_home

    override fun findViews() {
        viewPager = findViewById(R.id.viewPager)
        btnPage1 = findViewById(R.id.btn_page1)
        btnPage2 = findViewById(R.id.btn_page2)
        btnPage3 = findViewById(R.id.btn_page3)
        btnPage4 = findViewById(R.id.btn_page4)
    }


    override fun setListener() {
        val fragmentManager = supportFragmentManager
        val adapter = ViewPagerAdapter(fragmentManager)
        viewPager!!.adapter = adapter

        btnPage1!!.setOnClickListener(this)
        btnPage2!!.setOnClickListener(this)
        btnPage3!!.setOnClickListener(this)
        btnPage4!!.setOnClickListener(this)
    }

    override fun initData() {

        //todo list 到达主页面acitivty,需标记
        AppStatusProcessor.statusEnterHome(this, object : IEnterHomeCallback {
            override fun onClickUserPolicy() {
                //todo list 跳转用户协议，开发者自定义跳转页面及方式
                val bundle = Intent(this@HomeActivity, WebActivity::class.java)
                bundle.putExtra("type", 0)
                startActivity(bundle)
            }

            override fun onClickPrivacyPolicy() {
                //todo list 跳转隐私政策，开发者自定义跳转页面及方式
                val bundle = Intent(this@HomeActivity, WebActivity::class.java)
                bundle.putExtra("type", 1)
                startActivity(bundle)
            }
        })
    }


    override fun onClick(v: View) {
        super.onClick(v)
        when (v.id) {
            R.id.btn_page1 -> {
                viewPager!!.currentItem = 0
                updateButtonState(0)
            }

            R.id.btn_page2 -> {
                viewPager!!.currentItem = 1
                updateButtonState(1)
            }

            R.id.btn_page3 -> {
                viewPager!!.currentItem = 2
                updateButtonState(2)
            }

            R.id.btn_page4 -> {
                viewPager!!.currentItem = 3
                updateButtonState(3)
            }
        }

        OutIntAdApi.showSingleIntAd(this, "tab")
    }

    // 更新按钮选中状态（高亮当前页面按钮）
    private fun updateButtonState(selectedPosition: Int) {
        btnPage1!!.setBackgroundResource(if (selectedPosition == 0) R.mipmap.ict1 else R.mipmap.ict11)
        btnPage2!!.setBackgroundResource(if (selectedPosition == 1) R.mipmap.ict2 else R.mipmap.ict22)
        btnPage3!!.setBackgroundResource(if (selectedPosition == 2) R.mipmap.ict3 else R.mipmap.ict33)
        btnPage4!!.setBackgroundResource(if (selectedPosition == 3) R.mipmap.ict4 else R.mipmap.ict44)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        //todo list 热启动到达主页面，需标记
        AppStatusProcessor.statusOnIntentHome(this)
    }

    override fun onStop() {
        super.onStop()
        //todo list 主页面onStop,需标记
        AppStatusProcessor.statusStopHome()
    }

}